﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using GoShopping.Model;

namespace GoShopping.Web.ProductManager
{
    public partial class AddProduct1 : System.Web.UI.Page
    {
        BLL.ProductTypeBean ptb = new GoShopping.BLL.ProductTypeBean();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Master.ElementSelectedChange = GetTreeView;
                GetDDLData();
                GetTreeData();
            }
        }
                
        private void GetTreeView()
        {
            TreeView tv = Master.MasterTreeView;
            SetControlCommon.GetPrdtTreeView(tv, Server.MapPath("Navigation.xml"), "prdt");
        }        

        private IList<GoShopping.Model.ProductType> GetDDLData()
        {
            ddlParentName.Items.Clear();
            IList<GoShopping.Model.ProductType> lPt = ptb.GetAllProductTypeList();
            foreach (ProductType pt in lPt)
            {
                if (pt.ParentID != -1)
                {
                    continue;
                }
                ListItem li = new ListItem(pt.TypeName, pt.ID.ToString());
                ddlParentName.Items.Add(li);
            }
            return lPt;
        }

        protected void cbIsBaseType_CheckedChanged(object sender, EventArgs e)
        {
            if (cbIsBaseType.Checked)
            {
                ddlParentName.Enabled = false;
            }
            else
            {
                ddlParentName.Enabled = true;
            }
        }

        protected void btnClean_Click(object sender, EventArgs e)
        {
            zlTxtTypeName.Text = "";
            ddlParentName.SelectedIndex = -1;
            txtRemark.Text = ""; 
        }

        protected void btnDelType_Click(object sender, EventArgs e)
        {
            ArrayList al = new ArrayList();
            foreach (TreeNode tn in tvPrdtType.CheckedNodes)
            {
                if (tn.Checked)
                {
                    al.Add(tn.Value);
                }
            }
            if (al.Count == 0)
            {
                return;
            }
            for (int i = 0; i < al.Count; i++)
            {
                ptb.DelProductType(Convert.ToInt16(al[i]));
            }
            GetTreeData();
            tvPrdtType.ExpandAll();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveProductType();
            GetDDLData();
            GetTreeData();
            tvPrdtType.ExpandAll();
            //GetGVData();
        }
        
        protected void tvPrdtType_SelectedNodeChanged(object sender, EventArgs e)
        {
            hidId.Value = tvPrdtType.SelectedNode.Value;

            //string pareStr = tvPrdtType.SelectedNode.Parent.Value;
            zlTxtTypeName.Text = tvPrdtType.SelectedNode.Text;

            int index = 0;
            TreeNode tn = tvPrdtType.SelectedNode;
            foreach (ListItem li in ddlParentName.Items)
            {
                if (tn.Parent == null && tn.ChildNodes == null || tn.ChildNodes != null && tn.Parent == null)
                {
                    if (li.Value == tvPrdtType.SelectedNode.Value)
                    {
                        cbIsBaseType.Checked = true;
                        ddlParentName.Enabled = false;
                        break;
                    }
                }
                else
                {
                    if (li.Value == tvPrdtType.SelectedNode.Parent.Value)
                    {
                        cbIsBaseType.Checked = false;
                        ddlParentName.Enabled = true;
                        break;
                    }
                }
                index++;
            }
            ddlParentName.SelectedIndex = index;
            txtRemark.Text = tvPrdtType.ToolTip;
        }
    
        void GetTreeData()
        {
            tvPrdtType.Nodes.Clear();
            IList<Model.ProductType> iPT = ptb.GetAllProductTypeList();
            foreach (Model.ProductType pt in iPT)
            {
                if (pt.ParentID == -1)
                {
                    TreeNode tn = new TreeNode(pt.TypeName, pt.ID.ToString());
                    tn.ToolTip = pt.Remark;
                    tvPrdtType.Nodes.Add(tn);
                }
                else
                {
                    TreeNode tn = new TreeNode(pt.TypeName, pt.ID.ToString());
                    tn.ToolTip = pt.Remark;
                    TreeNode tnPare = tvPrdtType.FindNode(pt.ParentID.ToString());
                    tnPare.ChildNodes.Add(tn);
                }
            }
 
        }

        private void SaveProductType()
        {
            GoShopping.Model.ProductType pt = new GoShopping.Model.ProductType();
            pt.TypeName = zlTxtTypeName.Text.Trim();
            if (cbIsBaseType.Checked)
            {
                pt.ParentID = -1;
            }
            else
            {
                ddlParentName.Enabled = true;
                pt.ParentID = Convert.ToInt16(ddlParentName.SelectedItem.Value);//如何在页面元素中对应ParentID并获取
            }
            pt.Remark = txtRemark.Text.Trim();
            try
            {
                ptb.AddProductType(pt);
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('添加成功!')</script>", false);
            }
            catch
            {
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), null, "<script>alert('添加失败!')</script>", false);
            }
        }

        //protected void btnEdit_Click(object sender, EventArgs e)
        //{
        //    GoShopping.Model.ProductType pt = new GoShopping.Model.ProductType();
        //    if (string.IsNullOrEmpty(hidId.Value))
        //    {
        //        //这个类型不存在,请添加后再修改
        //        return;
        //    }
        //    pt.ID = Convert.ToInt16(hidId.Value);
        //    pt.TypeName = zlTxtTypeName.Text.Trim();
        //    if (cbIsBaseType.Checked)
        //    {

        //        pt.ParentID = -1;
        //    }
        //    else
        //    {
        //        ddlParentName.Enabled = true;
        //        pt.ParentID = Convert.ToInt16(ddlParentName.SelectedItem.Value);//如何在页面元素中对应ParentID并获取
        //    }
        //    pt.Remark = txtRemark.Text.Trim();
        //    try
        //    {
        //        ptb.UpdateOneProductType(pt);
        //    }
        //    catch
        //    {
        //        throw;
        //    }
        //    GetDDLData();
        //    GetTreeData();
        //    tvPrdtType.ExpandAll();
        //}
    }
}
